package com.renfei.multimediatask.ui.task8;

import android.graphics.SurfaceTexture;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.annotation.RequiresApi;

import com.renfei.multimediatask.R;
import com.renfei.multimediatask.base.BaseActivity;

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 视频解码，使用 videoDecodec.releaseOutputBuffers() 释放时，就会直接渲染到surface上，
 *
 */
public class VideoPlayMp4Activity extends BaseActivity implements TextureView.SurfaceTextureListener {


    private static final String TAG = VideoPlayMp4Activity.class.getSimpleName();
    @BindView(R.id.textureview)
    TextureView textureview;
    @BindView(R.id.startDecodec)
    Button startDecodec;
    @BindView(R.id.ll_btn_layout)
    RelativeLayout llBtnLayout;
    private String targetMp4;


    private Mp4MoviePlayer mp4MoviePlayer;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_play_mp4;
    }

    @Override
    protected void initListener() {
        targetMp4 = getParentFilePath() + "bank_xiaojiejie.mp4";

        textureview.setSurfaceTextureListener(this);
    }


    @OnClick(R.id.startDecodec)
    public void onViewClicked() {
        if (mp4MoviePlayer != null) {
            Log.w(TAG, "movie already playing");
            return;
        }
        SurfaceTexture st = textureview.getSurfaceTexture();
        Surface surface = new Surface(st);

        mp4MoviePlayer = new Mp4MoviePlayer(new File(targetMp4), surface);
        mp4MoviePlayer.start();
    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

}
